import { JsonConfigHelper } from "../../config/JsonConfigHelper";
import { G_Config } from "../../config/ConfigManager";
import { Task } from "../../app_base/module/task/Task";
import { ConfigHelper } from "../../app_base/module/config/ConfigHelper";

/**
 * 预加载配置列表
 */
const PRE_LOAD_CONFIGS:JsonConfigHelper<any>[] = [
    G_Config.level, //等级配置
    // game.config.shop, //商店配置
    //...
];

/**
 * 加载配置项
 */
 export class TaskLoadConfig extends Task{
    private _logLabel:string;
    /**
     * 开始加载配置项
     * @param preLoadList 资源列表
     */
    start(): void {
        if(!PRE_LOAD_CONFIGS.length){
            this.onComplete();
            return;
        }
        let loaded:number = 0;
        let onComplete = (error: Error, config: ConfigHelper<any>)=>{
            if(error){
                this.onError("LoadConfig", error.message);
                return;
            }
            if(++loaded >= PRE_LOAD_CONFIGS.length){
                this.onComplete();
            }else{
                this.onProgress(loaded/PRE_LOAD_CONFIGS.length);
            }
        }

        this._logLabel = `[预加载-配置表] ${PRE_LOAD_CONFIGS.length} ...done`;
        console.time(this._logLabel);
        PRE_LOAD_CONFIGS.forEach(config => {
            config.loadData(onComplete, this);
        });
    }
    
    onComplete(): void {
        console.timeEnd(this._logLabel);
        super.onComplete();
    }
}